package com.o2opuzi.pub.controller

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSON;
import com.o2opuzi.lib.db.Terms;
import com.o2opuzi.lib.util.ErrorMsgs
import com.o2opuzi.lib.util.FieldErrorUtils;
import com.o2opuzi.pub.dao.MenuDao;
import com.o2opuzi.pub.model.Menu;

@RestController
@RequestMapping("/admin/menu")
class MenuController {
	
	@Autowired
	private MenuDao menuDao;
	
	
	@GetMapping("/create")
	def create(){
		return [
			data:menuDao.getMenuTreeList()
		];
	}
	
	/**
	 * 新增菜单
	 * @auther Y.hj
	 * @param menu
	 * @return
	 */
	@PostMapping("/create")
	def  create(@RequestBody @Valid Menu menu,BindingResult result){
		//验证表单
		if(result.hasErrors()){
			return [code:0,messages:FieldErrorUtils.getErrors(result)];	
		}
		//验证菜单标识是否存在
		if(menuDao.exists("flag", menu.flag)){
			return [code:0,messages:["flag", "菜单标识已存在"]];
		}
		menuDao.save(menu);
		return [
			code:1,
			messages:[text:"新增成功"]
		];
	}
	
	/**
	 * 获取菜单
	 * @auther Y.hj
	 * @param id
	 * @return
	 */
	@GetMapping("/update")
	def get( Integer id){
		return [
			code:1,
			data:[
				menu:menuDao.findById(id),
				menu_list:menuDao.getMenuTreeList()
			]
		];
	}
	
	/**
	 * 获取说要菜单
	 * @auther Y.hj
	 * @param page
	 * @return
	 */
	@GetMapping("/get/all")
	def getAll(){
		return [
			code:1,
			data:menuDao.getMenuTreeList()
		];
	}
	
	/**
	 * 修改
	 * @auther Y.hj
	 * @param menu
	 * @return
	 */
	@PostMapping("/update")
	def update(@RequestBody @Valid Menu menu,BindingResult result){
		if(menu.id == null){
			return [code:0,messages:[text:"请选择要修改的菜单"]];
		}
		//验证表单
		if(result.hasErrors()){
			return [code:0,messages:FieldErrorUtils.getErrors(result)];
		}
		if(menuDao.find(new Terms(["flag","=",menu.flag],["id","<>",menu.id])) != null ){
			return [code:0,messages:[text:"菜单标识已存在"]];
		}
		
		if(menuDao.updateById(menu) > 0){
			return [
				code:1,
				messages:[text:"修改成功"]
			];
		}else{
			return [
				code:0,
				messages:[text:"修改失败"]
			];
		}
		
	}
	
	def subIdArray = {
		List list,Map map->
		List<Integer> idArray = [];
		list.each {
			if(map["id"] == it["parent_id"]){
				idArray.add(it["id"]);
				idArray.addAll(subIdArray(list,it));
			}
		}
		return idArray;
	}
	
	/**
	 * 删除
	 * @auther Y.hj
	 * @param menu
	 * @return
	 */
	@PostMapping("/delete")
	@Transactional
	def delete(@RequestBody Menu menu){
		if(menu.id == null){
			return [code:0,errors:[msg:"请选择要删除的菜单"]];
		}
		//当前菜单
		def m = menuDao.findForMap(menu.id);
		//所有的菜单
		def all = menuDao.findListForMap();
		List<Integer> idArray = subIdArray(all, m);	
		idArray.add(m["id"]);
		if(menuDao.deleteIn(idArray) > 0){
			return [
				code:1,
				messages:[text:"删除成功"]
			];
		}
		return [
			code:0,
			messages:[text:"删除失败"]
		];
	}
	
	
}
